# coding=utf-8
import ptx_lib
import PTX
import ERROR
import re
import sys
import getopt

# INIT
ptx = PTX.PTX()
current_bb = PTX.BasicBlocks()
current_kernel = PTX.Kernel()

dict_rd = {}


def filter_ptx_variables(line):
    """
    提取全局变量，格式化。 将.const .align 4 .b8 kernelTemplate[100] = {0, 0, 0, 0};形式转换成：
    ptx.global_varies[kernelTemplate] = {"const" = None, "align" = 4, ".b8" = True, array_len = 8, content = {0,0,0,0}
    """
    # @todo 如果没有 = ，那就应该是错误了吧
    if line.find('=') < 0:
        raise ERROR.NotSeenSyntax('global variable is not defined to array %s' % line)
        # 全局变量定义时不是数组
    first = line.split("=")
    # 去掉最后的分号
    first[1] = first[1][:-1]

    second = first[0].split()
    varies_name_reg = re.compile("(.+)\[(\d+)\]")
    varies_name = ""
    array_size = 0
    # 数组类型
    if varies_name_reg.findall(second[-1]):
        varies_name, array_size = varies_name_reg.findall(second[-1])[0]
    else:
        varies_name = varies_name_reg.findall(second[-1])[0]
    ptx.global_varies[varies_name] = {}
    # 如果是０，　应该是单一变量
    ptx.global_varies[varies_name]['array_size'] = array_size
    current_varies = ptx.global_varies[varies_name]
    current_varies['content'] = [x for x in re.split(",|}|{| ", first[1])]
    # @todo 添加其他属性的提取


def filter_reg_variables(line):
    """提取reg定义
    """
    line = line[:-1]
    line_split = line.split()[1:]

    variable_name_raw = line_split[-1]
    # reg_reg = re.compile("%(.+?)(<(\d+)>)*")
    reg_reg = re.compile("%(\w{1,2})(<(\d+)>)*")
    ans = reg_reg.findall(variable_name_raw)
    if not ans:
        raise ERROR.NotSeenSyntax("reg defined error： %s" % line)
    variable_name = ans[0]
    # if ans[2]:
    #  array_size = int(ans[2])
    # ptx.kernels[current_kernel].regs[variable_name] = PTX.Reg()
    # ptx.kernels[current_kernel].regs[variable_name].array_size = array_size

    for middle in line_split[:-1]:
        if middle not in ptx_lib.directives:
            raise ERROR.NotSeenSyntax("this attribute have not meet %s at line %s" % (middle, line))
        ptx.kernels[current_kernel].regs[variable_name].add(middle)


def read_files(file_name):
    global ptx
    # opts, file_name = getopt.getopt(argv, "hi:o")
    in_kernel = False
    with open(file_name, 'r')as fin:
        while 1:
            try:
                line = fin.readline()

                if not line:
                    break
                line = line.strip()
                # 如果以#开头，则可能是对外部程序的引用， @Manual 4.1
                # 但是目前的代码还没有见到@todo
                if line.startswith('.'):
                    # 用空格划分行
                    line_split = line.split()
                    identifier = line_split[0][1:]
                    # 判断属于什么header还是directives or params etc.
                    if identifier in ptx_lib.ptx_headers:
                        ptx.headers[identifier] = line_split[1]
                    elif identifier in ptx_lib.directives:
                        if identifier == 'const' and not in_kernel:
                            # 在nn的ptx代码中，是一个数组，但是如果还有不是的呢？？
                            filter_ptx_variables(line)
                        # 感觉这里单靠visible不可行
                        elif identifier == 'visible':
                            if line_split[-1][-1] == '(':
                                kernel_name = line_split[-1][:-1]
                            else:
                                kernel_name = line_split[-1]
                            ptx.kernels[kernel_name] = PTX.Kernel()
                            current_kernel = ptx.kernels[kernel_name]
                            current_kernel.entry_attributes = set(a[1:] for a in line_split if a.startswith('.'))
                            # raise NotSeenSyntax("not in kernel but %s" % line)
                        elif identifier == 'param':
                            if line_split[-1][-1] == ',':
                                param_name = line_split[-1][:-1]
                            else:
                                param_name = line_split[-1]
                            # current_kernel.params[param_name] = set(
                            #     a[1:] for a in line_split if a.startswith('.') and a != '.param')
                        elif in_kernel:
                            # 目前只看到了reg的情况
                            if identifier == 'reg':
                                filter_reg_variables(line)

                        else:
                            raise ERROR.NotSeenSyntax(
                                "this is directives,but not in kernel,is not visible or param cases, too")

                    else:
                        # 没有处理到的情况
                        raise ERROR.NotSeenSyntax(line)

                # CFG 没有啥可行的办法，先打印出来再说吧
                # synchronize instruction
                elif line.startswith('bar'):
                    print line
                elif line.startswith('mem'):
                    print line

                # control flow instruction {} @ bra ret exit
                # start of kernel
                elif line.startswith('{'):
                    print ' '
                    print '-------------------------kernel was launched -------------------------'
                    # 由于在.visible部分已经设置了current kernel 所以不需要了？？
                    in_kernel = True
                    # 可能还有/**/注释吗？？？
                elif line.startswith('}'):
                    print '-------------------------kernel was done -----------------------------'
                    print 'global define data'
                    # dict_rd.clear()
                    in_kernel = False

                elif line.startswith('@'):
                    print line
                elif line.startswith('bra'):
                    print line
                elif line.startswith('ret'):
                    print line
                elif line.startswith('exit'):
                    print line

                elif line.startswith("BB"):
                    print line

                # Comparison and Selection Instructions
                # set selp setp slct
                    """
                    CmpOp:
                    eq  a==b
                    ne  a!=b
                    lt  a< b
                    le  a<=b
                    gt  a> b
                    ge  a>=b
                    """
                elif line.startswith('set'):
                    line_split = line.split()
                    if len(line_split) == 4:
                        line_split[0] = line_split[0].split()

                    print line
                    ''' semantics
                    t = (a CmpOp b) ? 1 : 0;
                    if (isFloat(dtype))
                        d = BoolOp(t, c) ? 1.0f : 0x00000000;
                    else
                        d = BoolOp(t, c) ? 0xffffffff : 0x00000000;
                    '''
                elif line.startswith('selp'):
                    line_split = line.split()
                    # print line_split
                    # 去除无用 ,; 符号
                    line_split[1] = line_split[1].rstrip(',')
                    line_split[2] = line_split[2].rstrip(',')
                    line_split[3] = line_split[3].rstrip(',')
                    line_split[4] = line_split[4].rstrip(';')

                    if dict_rd.get(line_split[2]) and dict_rd.get(line_split[3]):
                        a = dict_rd[line_split[2]]
                        b = dict_rd[line_split[3]]
                    elif dict_rd.get(line_split[2]) and not dict_rd.get(line_split[3]):
                        a = dict_rd[line_split[2]]
                        b = line_split[3]
                    elif not dict_rd.get(line_split[2]) and dict_rd.get(line_split[3]):
                        a = line_split[2]
                        b = dict_rd[line_split[3]]
                    else:
                        a = line_split[2]
                        b = line_split[3]
                    if line_split[4] == 1:
                        dict_rd[line_split[1]] = a
                    else:
                        dict_rd[line_split[1]] = b
                    print line

                elif line.startswith('setp'):

                    print line
                elif line.startswith('slct'):
                    line_split = line.split()
                    # print line_split
                    # 去除无用 ,; 符号
                    line_split[1] = line_split[1].rstrip(',')
                    line_split[2] = line_split[2].rstrip(',')
                    line_split[3] = line_split[3].rstrip(',')
                    line_split[4] = line_split[4].rstrip(';')

                    if dict_rd.get(line_split[2]) and dict_rd.get(line_split[3]):
                        a = dict_rd[line_split[2]]
                        b = dict_rd[line_split[3]]
                    elif dict_rd.get(line_split[2]) and not dict_rd.get(line_split[3]):
                        a = dict_rd[line_split[2]]
                        b = line_split[3]
                    elif not dict_rd.get(line_split[2]) and dict_rd.get(line_split[3]):
                        a = line_split[2]
                        b = dict_rd[line_split[3]]
                    else:
                        a = line_split[2]
                        b = line_split[3]
                    if line_split[4] >= 0:
                        dict_rd[line_split[1]] = a
                    else:
                        dict_rd[line_split[1]] = b
                    print line

                elif line.startswith(')'):
                    pass

                elif line.startswith('\n') or line.startswith('#') or line.startswith('//'):
                    pass
                # ending of kernel

                    # dict_rd.clear()
                # match the 2 operands instruction
                elif line.startswith('mov'):
                    # split inst and operand
                    line_split = line.split()
                    # print line_split[1], line_split[2]
                    # 去除无用 ,; 符号
                    line_split[1] = line_split[1].rstrip(',')
                    line_split[2] = line_split[2].rstrip(';')
                    # mov.f32 % f291, % f292;
                    # print line_split
                    if dict_rd.get(line_split[2]):
                        dict_rd[line_split[1]] = dict_rd[line_split[2]]
                    else:
                        dict_rd[line_split[1]] = line_split[2]
                elif line.startswith('cvt'):
                    # split inst and operand
                    line_split = line.split()
                    # print line_split[1], line_split[2]
                    # 去除无用 ,; 符号
                    line_split[1] = line_split[1].rstrip(',')
                    line_split[2] = line_split[2].rstrip(';')
                    # mov.f32 % f291, % f292;
                    # print line_split
                    if dict_rd.get(line_split[2]):
                        dict_rd[line_split[1]] = dict_rd[line_split[2]]
                    else:
                        dict_rd[line_split[1]] = line_split[2]

                elif re.match('ld.*', line):

                    line_split = line.split()
                    # print line_split
                    # 去除无用 ,; 符号
                    line_split[1] = line_split[1].rstrip(',')
                    line_split[2] = line_split[2].rstrip(';')
                    # 去除 [ ]
                    line_split[2] = line_split[2].strip('[]')
                    # ld st 有偏移量怎么整 ？ split [r1 +5]
                    # 继续 split
                    if re.match('.*\+.*', line_split[2]):
                        line_split[2] = line_split[2].split('+')
                        if dict_rd.get(line_split[2][0]):
                            dict_rd[line_split[1]] = '(' + dict_rd[line_split[2][0]] + '+' + line_split[2][1] + ')'
                        else:
                            dict_rd[line_split[1]] = '(' + line_split[2][0] + '+' + line_split[2][1] + ')'
                    # print line_split[2]

                    elif dict_rd.get(line_split[2]):
                        dict_rd[line_split[1]] = dict_rd[line_split[2]]
                    else:
                        dict_rd[line_split[1]] = line_split[2]
                    if re.match('ld.*global.*', line):
                        print line_split[0], line_split[1], ' = ', dict_rd[line_split[1]]

                elif re.match('st.*', line):

                    line_split = line.split()
                    # print line_split
                    # 去除无用 ,; 符号
                    line_split[1] = line_split[1].rstrip(',')
                    line_split[2] = line_split[2].rstrip(';')
                    # ld st 有偏移量怎么整 ？ split [r1 +5]
                    # 去除 [ ]
                    line_split[1] = line_split[1].strip('[]')
                    # 继续 split or not
                    # print dict_rd[line_split[1]]
                    if re.match('.*\+.*', line_split[1]):
                        line_split[1] = line_split[1].split('+')
                        if dict_rd.get(line_split[1][0]):
                            dict_rd[line_split[2]] = '(' + dict_rd[line_split[1][0]] + '+' + line_split[1][1] + ')'
                        else:
                            dict_rd[line_split[2]] = '(' + line_split[1][0] + '+' + line_split[1][1] + ')'

                    elif dict_rd.get(line_split[1]):
                        dict_rd[line_split[2]] = dict_rd[line_split[1]]
                    else:
                        dict_rd[line_split[2]] = line_split[2]
                    # if dict_rd.get(line_split[2]):
                    #     pass
                    # else:
                    #     dict_rd[line_split[2]] = line_split[2]

                    if re.match('st.*global', line):
                        print line_split[0], '[', line_split[1], ' = ', dict_rd[line_split[2]], ']'
                        #  , '=', dict_rd[line_split[2]]

                elif line.startswith('add'):
                    line_split = line.split()
                    # print line_split
                    # 去除无用 ,; 符号
                    line_split[1] = line_split[1].rstrip(',')
                    line_split[2] = line_split[2].rstrip(',')
                    line_split[3] = line_split[3].rstrip(';')

                    if dict_rd.get(line_split[2]) and dict_rd.get(line_split[3]):
                        a = dict_rd[line_split[2]]
                        b = dict_rd[line_split[3]]
                    elif dict_rd.get(line_split[2]) and not dict_rd.get(line_split[3]):
                        a = dict_rd[line_split[2]]
                        b = line_split[3]
                    elif not dict_rd.get(line_split[2]) and dict_rd.get(line_split[3]):
                        a = line_split[2]
                        b = dict_rd[line_split[3]]
                    else:
                        a = line_split[2]
                        b = line_split[3]
                        # dict_rd[line_split[1]] = line_split[2] + '+' + line_split[3]
                    dict_rd[line_split[1]] = '(' + a + '+' + b + ')'
                elif line.startswith('and'):
                    line_split = line.split()
                    # print line_split
                    # 去除无用 ,; 符号
                    line_split[1] = line_split[1].rstrip(',')
                    line_split[2] = line_split[2].rstrip(',')
                    line_split[3] = line_split[3].rstrip(';')

                    if dict_rd.get(line_split[2]) and dict_rd.get(line_split[3]):
                        a = dict_rd[line_split[2]]
                        b = dict_rd[line_split[3]]
                    elif dict_rd.get(line_split[2]) and not dict_rd.get(line_split[3]):
                        a = dict_rd[line_split[2]]
                        b = line_split[3]
                    elif not dict_rd.get(line_split[2]) and dict_rd.get(line_split[3]):
                        a = line_split[2]
                        b = dict_rd[line_split[3]]
                    else:
                        a = line_split[2]
                        b = line_split[3]
                        # dict_rd[line_split[1]] = line_split[2] + '+' + line_split[3]
                    dict_rd[line_split[1]] = '(' + a + '&&' + b + ')'
                elif line.startswith('and'):
                    line_split = line.split()
                    # print line_split
                    # 去除无用 ,; 符号
                    line_split[1] = line_split[1].rstrip(',')
                    line_split[2] = line_split[2].rstrip(',')
                    line_split[3] = line_split[3].rstrip(';')

                    if dict_rd.get(line_split[2]) and dict_rd.get(line_split[3]):
                        a = dict_rd[line_split[2]]
                        b = dict_rd[line_split[3]]
                    elif dict_rd.get(line_split[2]) and not dict_rd.get(line_split[3]):
                        a = dict_rd[line_split[2]]
                        b = line_split[3]
                    elif not dict_rd.get(line_split[2]) and dict_rd.get(line_split[3]):
                        a = line_split[2]
                        b = dict_rd[line_split[3]]
                    else:
                        a = line_split[2]
                        b = line_split[3]
                        # dict_rd[line_split[1]] = line_split[2] + '+' + line_split[3]
                    dict_rd[line_split[1]] = '(' + a + '||' + b + ')'

                elif line.startswith('sub'):
                    line_split = line.split()
                    # print line_split
                    # 去除无用 ,; 符号
                    line_split[1] = line_split[1].rstrip(',')
                    line_split[2] = line_split[2].rstrip(',')
                    line_split[3] = line_split[3].rstrip(';')

                    if dict_rd.get(line_split[2]) and dict_rd.get(line_split[3]):
                        a = dict_rd[line_split[2]]
                        b = dict_rd[line_split[3]]
                    elif dict_rd.get(line_split[2]) and not dict_rd.get(line_split[3]):
                        a = dict_rd[line_split[2]]
                        b = line_split[3]
                    elif not dict_rd.get(line_split[2]) and dict_rd.get(line_split[3]):
                        a = line_split[2]
                        b = dict_rd[line_split[3]]
                    else:
                        a = line_split[2]
                        b = line_split[3]

                    dict_rd[line_split[1]] = '(' + a + '-' + b + ')'

                elif line.startswith('mul'):
                    line_split = line.split()
                    # print line_split
                    # 去除无用 ,; 符号
                    line_split[1] = line_split[1].rstrip(',')
                    line_split[2] = line_split[2].rstrip(',')
                    line_split[3] = line_split[3].rstrip(';')

                    if dict_rd.get(line_split[2]) and dict_rd.get(line_split[3]):
                        a = dict_rd[line_split[2]]
                        b = dict_rd[line_split[3]]
                    elif dict_rd.get(line_split[2]) and not dict_rd.get(line_split[3]):
                        a = dict_rd[line_split[2]]
                        b = line_split[3]
                    elif not dict_rd.get(line_split[2]) and dict_rd.get(line_split[3]):
                        a = line_split[2]
                        b = dict_rd[line_split[3]]
                    else:
                        a = line_split[2]
                        b = line_split[3]

                    dict_rd[line_split[1]] = '(' + a + '*' + b + ')'
                elif line.startswith('div'):
                    line_split = line.split()
                    # print line_split
                    # 去除无用 ,; 符号
                    line_split[1] = line_split[1].rstrip(',')
                    line_split[2] = line_split[2].rstrip(',')
                    line_split[3] = line_split[3].rstrip(';')

                    if dict_rd.get(line_split[2]) and dict_rd.get(line_split[3]):
                        a = dict_rd[line_split[2]]
                        b = dict_rd[line_split[3]]
                    elif dict_rd.get(line_split[2]) and not dict_rd.get(line_split[3]):
                        a = dict_rd[line_split[2]]
                        b = line_split[3]
                    elif not dict_rd.get(line_split[2]) and dict_rd.get(line_split[3]):
                        a = line_split[2]
                        b = dict_rd[line_split[3]]
                    else:
                        a = line_split[2]
                        b = line_split[3]

                    dict_rd[line_split[1]] = '(' + a + ' / ' + b + ')'
                elif line.startswith('rem'):
                    line_split = line.split()
                    # print line_split
                    # 去除无用 ,; 符号
                    line_split[1] = line_split[1].rstrip(',')
                    line_split[2] = line_split[2].rstrip(',')
                    line_split[3] = line_split[3].rstrip(';')

                    if dict_rd.get(line_split[2]) and dict_rd.get(line_split[3]):
                        a = dict_rd[line_split[2]]
                        b = dict_rd[line_split[3]]
                    elif dict_rd.get(line_split[2]) and not dict_rd.get(line_split[3]):
                        a = dict_rd[line_split[2]]
                        b = line_split[3]
                    elif not dict_rd.get(line_split[2]) and dict_rd.get(line_split[3]):
                        a = line_split[2]
                        b = dict_rd[line_split[3]]
                    else:
                        a = line_split[2]
                        b = line_split[3]

                    dict_rd[line_split[1]] = '(' + a + ' mod ' + b + ')'
                elif line.startswith('shr'):
                    line_split = line.split()
                    # print line_split
                    # 去除无用 ,; 符号
                    line_split[1] = line_split[1].rstrip(',')
                    line_split[2] = line_split[2].rstrip(',')
                    line_split[3] = line_split[3].rstrip(';')

                    if dict_rd.get(line_split[2]) and dict_rd.get(line_split[3]):
                        a = dict_rd[line_split[2]]
                        b = dict_rd[line_split[3]]
                    elif dict_rd.get(line_split[2]) and not dict_rd.get(line_split[3]):
                        a = dict_rd[line_split[2]]
                        b = line_split[3]
                    elif not dict_rd.get(line_split[2]) and dict_rd.get(line_split[3]):
                        a = line_split[2]
                        b = dict_rd[line_split[3]]
                    else:
                        a = line_split[2]
                        b = line_split[3]

                    dict_rd[line_split[1]] = '(' + a + '>>' + b + ')'

                elif line.startswith('shl'):
                    line_split = line.split()
                    # print line_split
                    # 去除无用 ,; 符号
                    line_split[1] = line_split[1].rstrip(',')
                    line_split[2] = line_split[2].rstrip(',')
                    line_split[3] = line_split[3].rstrip(';')

                    if dict_rd.get(line_split[2]) and dict_rd.get(line_split[3]):
                        a = dict_rd[line_split[2]]
                        b = dict_rd[line_split[3]]
                    elif dict_rd.get(line_split[2]) and not dict_rd.get(line_split[3]):
                        a = dict_rd[line_split[2]]
                        b = line_split[3]
                    elif not dict_rd.get(line_split[2]) and dict_rd.get(line_split[3]):
                        a = line_split[2]
                        b = dict_rd[line_split[3]]
                    else:
                        a = line_split[2]
                        b = line_split[3]
                        # dict_rd[line_split[1]] = line_split[2] + '+' + line_split[3]
                    dict_rd[line_split[1]] = '(' + a + '<<' + b + ')'
                elif line.startswith('mad') or line.startswith('fma'):
                    # mad.lo.s32	%r214, %r117, %r118, %r214;
                    line_split = line.split()
                    # print line_split
                    # 去除无用 ,; 符号
                    line_split[1] = line_split[1].rstrip(',')
                    line_split[2] = line_split[2].rstrip(',')
                    line_split[3] = line_split[3].rstrip(',')
                    line_split[4] = line_split[4].rstrip(';')
                    # 8 = 2^3 shit
                    # 000
                    if dict_rd.get(line_split[2]) and dict_rd.get(line_split[3]) and dict_rd.get(line_split[4]):
                        a = dict_rd[line_split[2]]
                        b = dict_rd[line_split[3]]
                        c = dict_rd[line_split[4]]
                    # 001
                    elif dict_rd.get(line_split[2]) and dict_rd.get(line_split[3]) and not dict_rd.get(line_split[4]):
                        a = dict_rd[line_split[2]]
                        b = dict_rd[line_split[3]]
                        c = line_split[4]
                    # 010
                    elif dict_rd.get(line_split[2]) and not dict_rd.get(line_split[3]) and dict_rd.get(line_split[4]):
                        a = dict_rd[line_split[2]]
                        b = line_split[3]
                        c = dict_rd[line_split[4]]
                    # 011
                    elif dict_rd.get(line_split[2]) and not dict_rd.get(line_split[3]) and not dict_rd.get(line_split[4]):
                        a = dict_rd[line_split[2]]
                        b = line_split[3]
                        c = line_split[4]
                    # 100
                    elif not dict_rd.get(line_split[2]) and dict_rd.get(line_split[3]) and dict_rd.get(line_split[4]):
                        a = line_split[2]
                        b = dict_rd[line_split[3]]
                        c = dict_rd[line_split[4]]
                    # 101
                    elif not dict_rd.get(line_split[2]) and dict_rd.get(line_split[3]) and not dict_rd.get(line_split[4]):
                        a = line_split[2]
                        b = dict_rd[line_split[3]]
                        c = line_split[4]
                    # 110
                    elif not dict_rd.get(line_split[2]) and not dict_rd.get(line_split[3]) and dict_rd.get(line_split[4]):
                        a = line_split[2]
                        b = line_split[3]
                        c = dict_rd[line_split[4]]
                    # 111
                    else:
                        a = line_split[2]
                        b = line_split[3]
                        c = line_split[4]
                    dict_rd[line_split[1]] = '(' + a + '*' + b + '+' + c + ')'

                # 没匹配到？？？
                # max and min 值不确定 怎么比大小？
                # 只能两个都 搞出来了。。
                elif line.startswith('max'):
                    line_split = line.split()
                    # print line_split
                    # 去除无用 ,; 符号
                    line_split[1] = line_split[1].rstrip(',')
                    line_split[2] = line_split[2].rstrip(',')
                    line_split[3] = line_split[3].rstrip(';')

                    if dict_rd.get(line_split[2]) and dict_rd.get(line_split[3]):
                        a = dict_rd[line_split[2]]
                        b = dict_rd[line_split[3]]
                    elif dict_rd.get(line_split[2]) and not dict_rd.get(line_split[3]):
                        a = dict_rd[line_split[2]]
                        b = line_split[3]
                    elif not dict_rd.get(line_split[2]) and dict_rd.get(line_split[3]):
                        a = line_split[2]
                        b = dict_rd[line_split[3]]
                    else:
                        a = line_split[2]
                        b = line_split[3]

                    dict_rd[line_split[1]] = '(a>b?' + a + ':' + b + ')'

                elif line.startswith('min'):
                    line_split = line.split()
                    # print line_split
                    # 去除无用 ,; 符号
                    line_split[1] = line_split[1].rstrip(',')
                    line_split[2] = line_split[2].rstrip(',')
                    line_split[3] = line_split[3].rstrip(';')

                    if dict_rd.get(line_split[2]) and dict_rd.get(line_split[3]):
                        a = dict_rd[line_split[2]]
                        b = dict_rd[line_split[3]]
                    elif dict_rd.get(line_split[2]) and not dict_rd.get(line_split[3]):
                        a = dict_rd[line_split[2]]
                        b = line_split[3]
                    elif not dict_rd.get(line_split[2]) and dict_rd.get(line_split[3]):
                        a = line_split[2]
                        b = dict_rd[line_split[3]]
                    else:
                        a = line_split[2]
                        b = line_split[3]
                    dict_rd[line_split[1]] = '(a<b?' + a + ':' + b + ')'
            except ERROR.NotSeenSyntax as e:
                print(e)


# read_files(sys.argv[1:])
read_files('/home/zagncq/Shared/BH/main_rmv.ptx')

# 分割线
# print '---------------------fen ge xian ----------------------------------------'
# for key in dict_rd.keys():
#     print key, '=', dict_rd[key]
